package cn.jdy.anquan.web;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.anquan.entity.Survey;
import cn.jdy.anquan.service.SurveyService;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;

@Controller
@RequestMapping("/survey")
public class SurveyCtrl extends BaseCtrl {
  @Autowired
  private SurveyService surveyService;
  
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  @ResponseBody
  public ModelAndView save(Survey survey){
    try {
      survey.setCreateTime(DateUtil.getCurrentDate2());
      survey.setIp(request.getRemoteAddr());
      surveyService.save(survey);
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "投票失败", e);
    }
    return new ModelAndView("redirect:/survey/look");
  }
  
  @RequestMapping(value = "/look", method = RequestMethod.GET)
  public ModelAndView query(Survey survey){
    ModelAndView mv = new ModelAndView("/help/lookGusebook.jsp");
    List<Survey> list = surveyService.query();
    int count = surveyService.getCount();
    mv.addObject("count", count);
    
    String scale = "0%";
    Survey sur1 = new Survey();
    Survey sur2= new Survey();
    Survey sur3 = new Survey();
    sur1.setScale(scale);
    sur1.setTotal(0);
    sur1.setResult(1L);
    sur2.setScale(scale);
    sur2.setTotal(0);
    sur2.setResult(1L);
    sur3.setScale(scale);
    sur3.setTotal(0);
    sur3.setResult(1L);
    
    if(count!=0){
      for(Survey s:list){
        
        if(s.getResult()==1){
          float sc= (float)s.getTotal()/(float)count*100;
          DecimalFormat df2 = new DecimalFormat("#"); 
          sur1.setScale(df2.format(sc)+"%");
          sur1.setResult(s.getResult());
          sur1.setTotal(s.getTotal());
        }else if(s.getResult()==2){
          float sc= (float)s.getTotal()/(float)count*100;
          DecimalFormat df2 = new DecimalFormat("#"); 
          sur2.setScale(df2.format(sc)+"%");
          sur2.setResult(s.getResult());
          sur2.setTotal(s.getTotal());
        }else if(s.getResult()==3){
          float sc= (float)s.getTotal()/(float)count*100;
          DecimalFormat df2 = new DecimalFormat("#"); 
          sur3.setScale(df2.format(sc)+"%");
          sur3.setResult(s.getResult());
          sur3.setTotal(s.getTotal());
        }
       
      }
    }
    List<Survey> list2 = new ArrayList<Survey>();
    
    list2.add(sur1);
    list2.add(sur2);
    list2.add(sur3);
    
    mv.addObject("list", list2);
     
    return mv;
  }
  
}
